/*
 * @Description: cms问卷解析-hook
 * @Author: xinqiao lxq1990.0716@qq.com
 * @Date: 2024-04-16
 * @LastEditors: xinqiao lxq1990.0716@qq.com
 * @LastEditTime: 2024-04-20
 */

import { ref } from 'vue'

import { requestSaveAnswer } from '@/api/business/tracecode/detectionInfo'
import { requestCheckVoucher, requestGetMyPaperDetail, requestUnAuthApiJson } from '@/api/content/survey'
import { HttpCodeEnum } from '@/enum/httpEnum'

import { CmsResStatusEnum } from '../enum/enum'
import type { ICmsAnalysisPrepare, ICmsPaper, ICmsPreviewPrepare, ICmsSubmitPaperRes } from '../type/type'
import { convertAnalysisAnswer, convertPaper } from './convertAnalysis'
import { convertApiAnswer } from './convertApi'

/**
 * cms问卷解析
 */
export const useCmsAnalysis = () => {
  /** 发布问卷id */
  const publishId = ref('')
  const detectionInfoId = ref('')
  /** 用户id */
  const userId = ref('')
  /** 昵称 */
  const nickname = ref('')
  /** detectionType 问卷类型 */
  const detectionType = ref(null)

  /** 问卷内容 */
  const paper = ref({} as ICmsPaper)
  /** 答卷id */
  const recordId = ref('')

  /**
   * 准备问卷数据
   * @param data 准备数据
   */
  const preparePaper = (data: ICmsAnalysisPrepare) => {
    publishId.value = data.publishId
    detectionInfoId.value = data.detectionInfoId
    nickname.value = data.nickname
    userId.value = data.userId
    detectionType.value = data.detectionType
  }

  /**
   * 解析问卷模板
   * @returns
   */
  const analysisPaperTemplate = async () => {
    const { responseCode, responseData } = await requestUnAuthApiJson(publishId.value)
    if (responseCode === HttpCodeEnum.SUCCESS) {
      paper.value = convertPaper(responseData)
    }
  }

  /**
   * 创建一份空的答卷
   * @param nickname 昵称
   * @param publishId 发布问卷的formid
   * @param userId 用户id
   * @returns
   */
  const createOnePaper = async () => {
    const { responseCode, responseData } = await requestCheckVoucher({
      publishId: publishId.value,
      userId: userId.value,
      nickname: nickname.value,
    })
    if (responseCode === HttpCodeEnum.SUCCESS) {
      recordId.value = responseData.recordId
    } else {
      uni.showModal({
        title: '该问卷已下架!',
        icon: 'none',
        confirmText: '确定',
        confirmColor: '#08979C',
        showCancel: false,
        success: () => {
          uni.navigateBack()
        },
      })
    }
  }

  /**
   * 提交答卷
   * @param paper 答卷内容
   * @param recordId 答卷id
   */
  const submitPaper = async (): Promise<ICmsSubmitPaperRes<SaveAnswerTypes.IResponse>> => {
    const result = {} as SaveAnswerTypes.IRequest
    result.publishId = publishId.value
    result.detectionInfoId = detectionInfoId.value
    result.questionnaireRecordId = recordId.value
    result.answers = convertApiAnswer(paper.value.questions)
    result.status = 0 //传默认值0
    result.detectionType = detectionType.value //检测类型 1-肠道菌群检测
    const { responseCode, responseData, responseMessage } = await requestSaveAnswer(result)
    if (responseCode === HttpCodeEnum.SUCCESS) {
      return { status: CmsResStatusEnum.SUCCESS, data: responseData, message: responseMessage }
    }
    return { status: CmsResStatusEnum.ERROR, data: responseData, message: responseMessage }
  }

  return {
    paper,
    preparePaper,
    analysisPaperTemplate,
    createOnePaper,
    submitPaper,
  }
}

/**
 * cms问卷预览
 */
export const useCmsPreview = () => {
  /** 发布问卷id */
  const publishId = ref('')
  /** 答卷id */
  const recordId = ref('')
  /** 用户id */
  const userId = ref('')

  /** 问卷内容 */
  const paper = ref({} as ICmsPaper)

  /**
   * 准备问卷数据
   * @param data 准备数据
   */
  const preparePaper = (data: ICmsPreviewPrepare) => {
    publishId.value = data.publishId
    recordId.value = data.recordId
    userId.value = data.userId
  }

  /**
   * 解析问卷模板
   * @returns
   */
  const analysisPaperTemplate = async () => {
    const { responseCode, responseData } = await requestUnAuthApiJson(publishId.value)
    if (responseCode === HttpCodeEnum.SUCCESS) {
      paper.value = convertPaper(responseData)
      console.log('analysisPaperTemplate: ', paper.value)
    }
  }

  /**
   * 解析答案
   */
  const analysisAnswer = async () => {
    const { responseData, responseCode } = await requestGetMyPaperDetail({
      userId: userId.value,
      recordId: recordId.value,
    })
    if (responseCode === HttpCodeEnum.SUCCESS) {
      const answers = JSON.parse(responseData.answerText)
      paper.value.questions = convertAnalysisAnswer(paper.value.questions, answers)
    }
  }

  return {
    paper,
    preparePaper,
    analysisPaperTemplate,
    analysisAnswer,
  }
}
